//
// Created by Administrator on 2025/3/5.
//

#include "ui_word_quey_main_screen.h"
static void fresh_letter_by_parent_index(int index);

static void fresh_letter_parent_bottom_border_style(lv_obj_t *pObj);
static void fresh_recently_word_list(word_query_word_entry_t *word_list,int size,lv_obj_t *prev_obj,int pad_row);

static void fresh_input_box_panel();


static void ui_fresh_search_word_list();

static void ui_hidden_input_box_dash_img(lv_obj_t *obj,bool is_hidden);
static void fresh_parent_letter_checked(lv_obj_t *obj);


char *word_list_label_text[] = {"最近查询单词:","clean 打扫","surprise 惊奇","hungry 饥饿的",
                                        "thirsty 口渴的","picnic 野餐"};

// 左键处理
static void ui_word_query_left_key(lv_group_t *group,lv_obj_t *obj,word_query_event_data_t* event_data){
    int index = event_data->index;
    int obj_type = event_data->obj_type;
    LOG_INFO_WRAPPER("start index:%d obj_type:%d", index,obj_type);
    /**
     * 1.如果type是letter
     * 并且index>0,则focus到上一个字母
     *  index=0,则focus到上一个parent
     * 2.如果type是parent
     *  先不处理
     */

    if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_CHILD){
        // 取消parent的checked 恢复dash_img，选中上一个的checked，隐藏上一个的dash_img
        lv_obj_t *current_parent = event_data->extra_obj;
        lv_obj_t *current_dash_img = lv_obj_get_child(current_parent, 0);
        lv_obj_clear_state(current_parent, LV_STATE_CHECKED);
        lv_obj_clear_flag(current_dash_img, LV_OBJ_FLAG_HIDDEN);
        if(index > 0){
            lv_group_focus_obj(event_data->prev_obj);
        }else{
            lv_group_focus_obj(event_data->prev_parent_obj);
        }
    }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_PARENT){
        /**
         * 上一个obj不为空，则focus到上一个obj，并且刷新bottom_border样式
         */
         if(event_data->prev_obj){
             lv_group_focus_obj(event_data->prev_obj);
         }

    }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_WORD_LIST_CHILD){
        lv_group_focus_obj(event_data->prev_obj);
    }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_INPUT_BOX_PANEL){
        // 需要显示img
        ui_hidden_input_box_dash_img(obj,false);
        lv_group_focus_obj(event_data->prev_obj);
    }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_DELETE_PANEL){
        // 取消parent checked 显示dashimg
        ui_hidden_input_box_dash_img(obj,false);
        lv_group_focus_obj(event_data->prev_obj);
    }

}


//右键处理
static void ui_word_query_right_key(lv_group_t *group,lv_obj_t *obj,word_query_event_data_t* event_data){
    int index = event_data->index;
    int obj_type = event_data->obj_type;
    LOG_INFO_WRAPPER("start index:%d obj_type:%d", index,obj_type);
    /**
     * 1.如果type是letter
     *  并且index < letter_child_size-1,则focus到下一个字母
     *  index = letter_child_size-1,则focus到parent的下一个节点
     */
     if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_CHILD){
         // 取消parent的checked 恢复dash_img，选中下一个的checked，隐藏下一个的dash_img
         lv_obj_t *current_parent = event_data->extra_obj;
         lv_obj_t *current_dash_img = lv_obj_get_child(current_parent, 0);
         lv_obj_clear_state(current_parent, LV_STATE_CHECKED);
         lv_obj_clear_flag(current_dash_img, LV_OBJ_FLAG_HIDDEN);
         if(index < event_data->letter_size-1){
             // 下一个parent的event_data
             lv_group_focus_obj(event_data->next_obj);
         }else{
             lv_group_focus_obj(event_data->next_parent_obj);

         }
     }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_PARENT){
         /**
         * 下一个obj不为空，则focus到下一个obj，并且刷新bottom_border样式
         */
         if(event_data->next_obj){
             lv_group_focus_obj(event_data->next_obj);
         }
     }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_WORD_LIST_CHILD){
         lv_group_focus_obj(event_data->next_obj);
     }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_INPUT_BOX_PANEL){
         // 显示img
         ui_hidden_input_box_dash_img(obj, false);
         lv_group_focus_obj(event_data->next_obj);
     }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_TOP_PANEL_LABEL){
         // 判断带不带删除panel
         lv_group_focus_obj(event_data->next_obj);

     }else if(obj_type == UI_COMP_WORD_QUERY_INPUT_DELETE_BUTTON_PANEL){
         ui_hidden_input_box_dash_img(obj,false);
         lv_group_focus_obj(event_data->next_obj);
     }
}



//确定处理
static void ui_word_query_enter_key(lv_group_t *group,lv_obj_t *obj,word_query_event_data_t* event_data){
    int index = event_data->index;
    int obj_type = event_data->obj_type;
    LOG_INFO_WRAPPER("start index:%d obj_type:%d", index,obj_type);

    if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_CHILD){
        // 输出字母
        lv_obj_t *letter_label = lv_obj_get_child(obj, 0);
        if(letter_label->class_p == &lv_label_class){
            char *letter_str = lv_label_get_text(letter_label);
            LOG_INFO_WRAPPER("letter:%s",letter_str);
            // 将letter_str追加
            if(word_query_module.data->input_word_len < INPUT_BOX_MAX_LEN){
                if(strcmp(letter_str,"空")){
                    strcat(word_query_module.data->input_word, letter_str);
                }else{
                    strcat(word_query_module.data->input_word, " ");
                }
                word_query_module.data->input_word_len++;
                LOG_INFO_WRAPPER("input_word:%s",word_query_module.data->input_word);
                ui_fresh_search_word_list();

            }

        }


    }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_PARENT){
        // 刷新letter列表和checked 父项
        LOG_INFO_WRAPPER("current_parent_index:%d selected_parent_index:%d",
                         word_query_module.data->letter_parent_index, index);
        word_query_module.data->letter_parent_index = index;
        fresh_letter_by_parent_index(word_query_module.data->letter_parent_index);
    }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_DELETE_PANEL){
        // 将word_query_module.data->input_word的长度减一
        word_query_module.data->input_word[word_query_module.data->input_word_len-1] = '\0';
        word_query_module.data->input_word_len--;
        LOG_INFO_WRAPPER("input_word:%s",word_query_module.data->input_word);
        ui_fresh_search_word_list();

        if(word_query_module.data->input_word_len == 0){
            // focus 输入框，隐藏dashimg
            lv_obj_t *input_panel = event_data->next_parent_obj;
            lv_group_focus_obj(input_panel);
            // 去除删除的parent的checked,显示dash_img
            lv_obj_t *delete_panel = lv_obj_get_parent(obj);
            lv_obj_clear_state(delete_panel, LV_STATE_CHECKED);
            lv_obj_t *dash_img = lv_obj_get_child(delete_panel, 0);
            lv_obj_clear_flag(dash_img, LV_OBJ_FLAG_HIDDEN);
        }

    }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_TOP_PANEL_LABEL)
    {
        LOG_INFO_WRAPPER("UI_WORD_QUERY_OBJ_TYPE_TOP_PANEL_LABEL");
        word_query_module.screen->deinit_cb();
    }
}




static void ui_word_query_main_screen_event(lv_event_t *e){
    LOG_INFO_WRAPPER("start");
//    lv_group_send_data()
    lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_target(e);
    lv_group_t *group = lv_group_get_default();
    word_query_event_data_t * event_data = (word_query_event_data_t *)lv_event_get_user_data(e);
    if (event_code == LV_EVENT_KEY)
    {
        // 按键事件 获取按键值
        uint32_t lv_key = lv_event_get_key(e);
        switch (lv_key)
        {
            case LV_KEY_UP:
                LV_LOG_USER("=========== touched KeyUp");

                break;
            case LV_KEY_DOWN:
                LV_LOG_USER("============ touched KeyDown");

                break;
            case LV_KEY_LEFT:
                LV_LOG_USER("=========== touched KeyLeft");
                ui_word_query_left_key(group,target,event_data);
//                lv_group_focus_prev(group);

                break;
            case LV_KEY_RIGHT:
                LV_LOG_USER("============ touched KeyRight");
                ui_word_query_right_key(group, target, event_data);
//                lv_group_focus_next(group);

                break;
            case LV_KEY_ENTER:
                LV_LOG_USER("============ touched KeyEnter");
                ui_word_query_enter_key(group,target,event_data);

                break;
            case LV_KEY_BACKSPACE:
                LV_LOG_USER("============ touched KeyBackspace");

                break;
            case LV_KEY_ESC:
                LV_LOG_USER("============ touched ESC");
                word_query_keyboard_switch_dao();
                break;
            default:
                break;
        }
    }
}

// 焦点组回调
void ui_word_query_focus_cb(lv_group_t *group){
    LOG_INFO_WRAPPER("start");
    lv_obj_t *focus_obj = lv_group_get_focused(group);
    if(focus_obj){
        // user_data
        word_query_event_data_t *user_data = lv_obj_get_user_data(focus_obj);
        if(user_data){
            int obj_type = user_data->obj_type;
            if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_CHILD){
                LOG_INFO_WRAPPER("UI_WORD_QUERY_OBJ_TYPE_LETTER_CHILD");
                lv_obj_t *prev_parent = user_data->extra_obj;
                lv_obj_t *prev_dash_img = lv_obj_get_child(prev_parent, 0);
                lv_obj_add_state(prev_parent, LV_STATE_CHECKED);
                lv_obj_add_flag(prev_dash_img, LV_OBJ_FLAG_HIDDEN);
            }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_LETTER_PARENT){
                LOG_INFO_WRAPPER("UI_WORD_QUERY_OBJ_TYPE_LETTER_PARENT");
                fresh_parent_letter_checked(focus_obj);
            }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_INPUT_BOX_PANEL){
                lv_obj_t *dash_img = lv_obj_get_child(focus_obj, 0);
                lv_obj_add_flag(dash_img, LV_OBJ_FLAG_HIDDEN);
            }else if(obj_type == UI_WORD_QUERY_OBJ_TYPE_DELETE_PANEL){
                lv_obj_t *delete_parent_panel = user_data->prev_parent_obj;
                lv_obj_t *dash_img = lv_obj_get_child(delete_parent_panel, 0);
                lv_obj_add_state(delete_parent_panel, LV_STATE_CHECKED);
                lv_obj_add_flag(dash_img, LV_OBJ_FLAG_HIDDEN);
            }
        }
    }
}

// 添加事件
static void word_query_add_event_common(lv_obj_t *obj,word_query_event_data_t * event_data){
    lv_group_add_obj(word_query_module.screen->group,obj);
    // 设置 user_data（存储 event_data）
    if(event_data){
        lv_obj_set_user_data(obj, event_data);
        lv_obj_add_event_cb(obj, common_del_event_data_event_cb, LV_EVENT_DELETE,
                            event_data);
    }
    // 删除事件时回调
    lv_obj_add_event_cb(obj, ui_word_query_main_screen_event, LV_EVENT_KEY,
                        event_data);

}

// 当前obj是否被选中，选中择去除checked状态，没选中则添加checked状态
static void fresh_parent_letter_checked(lv_obj_t *obj){
    if(obj){
        word_query_event_data_t *event_data = lv_obj_get_user_data(obj);
        if(word_query_module.data->letter_parent_index == event_data->index){
            lv_obj_add_state(obj, LV_STATE_CHECKED);
        }else{
            lv_obj_clear_state(obj, LV_STATE_CHECKED);
        }
    }
}

static void fresh_letter_parent_bottom_border_style(lv_obj_t * current_obj){
    LOG_INFO_WRAPPER("start");
    word_query_event_data_t *event_data = lv_obj_get_user_data(current_obj);

    if(event_data){
        // 当前obj是否被选中，选中择去除checked状态，没选中则添加checked状态
        fresh_parent_letter_checked(current_obj);
//        fresh_parent_letter_checked(event_data->prev_obj);
//        fresh_parent_letter_checked(event_data->next_obj);
//        if(word_query_module.data->letter_parent_index == event_data->index){
//            lv_obj_clear_state(current_obj, LV_STATE_CHECKED);
//        }else{
//            lv_obj_add_state(current_obj, LV_STATE_CHECKED);
//        }
//        // 去除上一个节点和下一个节点的checked状态
//        if(event_data->prev_obj){
//            lv_obj_clear_state(event_data->prev_obj, LV_STATE_CHECKED);
//        }
//        if(event_data->next_obj){
//            lv_obj_clear_state(event_data->next_obj, LV_STATE_CHECKED);
//        }
    }
}

// 刷新字符列表的父节点样式
static void fresh_letter_parent_style(int index){
    LOG_INFO_WRAPPER("start");
    if(index < 0 || index >= word_query_module.data->letter_parent_size){
        LOG_ERROR_WRAPPER("index error");
        return;
    }
    lv_obj_t *middle_right_left_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                                 UI_COMP_WORD_QUERY_LETTER_PARENT_PANEL);
    for (int i = 0; i < word_query_module.data->letter_parent_size; ++i) {
        lv_obj_t *child_panel = lv_obj_get_child(middle_right_left_panel,i);
        lv_obj_t *child_label = lv_obj_get_child(lv_obj_get_child(child_panel,0), 0);
        if(i == index){
            lv_obj_add_state(child_panel, LV_STATE_CHECKED);

            lv_obj_add_state(child_label, LV_STATE_CHECKED);
        }else{
            lv_obj_clear_state(child_panel, LV_STATE_CHECKED);
            lv_obj_clear_state(child_label, LV_STATE_CHECKED);
        }
    }
}

// 根据父节点索引刷新字母列表
static void fresh_letter_by_parent_index(int index){
    LOG_INFO_WRAPPER("start index:%d",index);
    if(index <0 || index >= word_query_module.data->letter_parent_size){
        LOG_ERROR_WRAPPER("index error");
        return;
    }
    // 获取父节点
    lv_obj_t *middle_right_right_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                             UI_COMP_WORD_QUERY_LETTER_PARENT_LIST_PANEL);
    // 获取父项
    lv_obj_t *middle_right_left_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                                 UI_COMP_WORD_QUERY_LETTER_PARENT_PANEL);
    lv_obj_t *prev_parent_panel = NULL;
    lv_obj_t *next_parent_panel = NULL;

    prev_parent_panel = lv_obj_get_child(lv_obj_get_child(middle_right_left_panel,index),0);

    if(index != word_query_module.data->letter_parent_size-1){
        next_parent_panel = lv_obj_get_child(lv_obj_get_child(middle_right_left_panel,index+1),0);
    }else{
        next_parent_panel = lv_obj_get_child(lv_obj_get_child(middle_right_left_panel,index),0);
    }
    char **middle_right_right_label_text = NULL;
    int middle_right_right_panel_ele_num = 0;
    // 清除子节点
    lv_obj_clean(middle_right_right_panel);

    // 上一个和下一个节点letter数量
    int prev_parent_letter_size = 0;
    int next_parent_letter_size = 0;

    // 根据下标填充节点
    if(index == 0){
        middle_right_right_label_text = lowercase_letter_strings;
        middle_right_right_panel_ele_num = lowercase_letter_strings_size;
        next_parent_letter_size = uppercase_letter_strings_size;

    }else if(index == 1){
        middle_right_right_label_text = uppercase_letter_strings;
        middle_right_right_panel_ele_num = uppercase_letter_strings_size;
        prev_parent_letter_size = lowercase_letter_strings_size;
        next_parent_letter_size = digit_strings_size;
    }else if(index == 2){
        middle_right_right_label_text = digit_strings;
        middle_right_right_panel_ele_num = digit_strings_size;
        prev_parent_letter_size = uppercase_letter_strings_size;
        next_parent_letter_size = special_characters_size;
    }else if(index == 3){
        middle_right_right_label_text = special_characters;
        middle_right_right_panel_ele_num = special_characters_size;
        prev_parent_letter_size = digit_strings_size;
    }

    word_query_module.data->letter_child_size = middle_right_right_panel_ele_num;

    // 在event_data中保存上一个和下一个middle_right_right_child_panel
    lv_obj_t *prev_middle_right_right_child_panel = NULL;

    for (int i = 0; i < middle_right_right_panel_ele_num; ++i) {
        lv_obj_t *middle_right_right_child_parent_panel = ui_create_without_flex_panel(middle_right_right_panel, 25, 28);
        lv_obj_set_style_border_width(middle_right_right_child_parent_panel, 1, LV_STATE_CHECKED);
        lv_obj_set_style_border_side(middle_right_right_child_parent_panel, LV_BORDER_SIDE_FULL, LV_STATE_CHECKED);
//        lv_obj_t *middle_right_right_child_img = ui_create_default_image_by_src(middle_right_right_panel,
//                                                                                &dash_25_28_img);
        lv_obj_t *middle_right_right_child_img = ui_create_default_image_by_src(middle_right_right_child_parent_panel,
                                                                                &dash_25_28_img);
        lv_coord_t panel_width = 15;
        lv_coord_t panel_height = 24;
        lv_coord_t pad_top = -6;
        lv_coord_t border_width = 2;
        lv_coord_t pad_left = -2;

        // 根据条件修改上述值
        if(!strncmp(middle_right_right_label_text[i],"空",10)){
            panel_width = 16;
            pad_left = 0;
        }

        lv_obj_t *middle_right_right_child_panel = ui_create_without_flex_panel(middle_right_right_child_parent_panel,panel_width,panel_height);
        // 事件数据
        word_query_event_data_t *event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        event_data->index = i;
        event_data->obj_type = UI_WORD_QUERY_OBJ_TYPE_LETTER_CHILD;
        event_data->prev_obj = prev_middle_right_right_child_panel;
        event_data->next_obj = NULL;
        event_data->prev_parent_obj = prev_parent_panel;
        event_data->next_parent_obj = next_parent_panel;
        event_data->letter_size = middle_right_right_panel_ele_num;
        event_data->prev_child_size = prev_parent_letter_size;
        event_data->next_child_size = next_parent_letter_size;
        event_data->extra_obj = middle_right_right_child_parent_panel;
        // 让前一个 panel 的 next 指向当前 panel
        if (prev_middle_right_right_child_panel) {

            word_query_event_data_t *prev_event_data = lv_obj_get_user_data(prev_middle_right_right_child_panel);
            if (prev_event_data) {
                prev_event_data->next_obj = middle_right_right_child_panel;
            }
        }
        word_query_add_event_common(middle_right_right_child_panel,event_data);
//        lv_group_add_obj(word_query_module.screen->group, middle_right_right_child_panel);
//        // 设置 user_data（存储 event_data）
//        lv_obj_set_user_data(middle_right_right_child_panel, event_data);
//        lv_obj_add_event_cb(middle_right_right_child_panel, ui_word_query_main_screen_event, LV_EVENT_KEY, event_data);
//        // 删除事件时回调
//        lv_obj_add_event_cb(middle_right_right_child_panel, common_del_event_data_event_cb, LV_EVENT_DELETE, event_data);
        // 顶部对齐
        lv_obj_set_align(middle_right_right_child_panel, LV_ALIGN_CENTER);
        lv_obj_set_style_pad_top(middle_right_right_child_panel, pad_top, 0);
        lv_obj_set_style_border_side(middle_right_right_child_panel, LV_BORDER_SIDE_BOTTOM, LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(middle_right_right_child_panel, border_width, LV_STATE_FOCUSED);
//            lv_obj_set_style_pad_left(middle_right_right_child_panel, pad_left, LV_STATE_FOCUSED);
        lv_obj_t *middle_right_right_child_label = ui_create_default_label(middle_right_right_child_panel,middle_right_right_label_text[i],CHINESE_FONT_16_LIGHT);

        // 更新 prev 变量，为下一次循环做准备
        prev_middle_right_right_child_panel = middle_right_right_child_panel;

    }

    // focus第一个字母
    lv_obj_t *focus_obj_img_parent = lv_obj_get_child(middle_right_right_panel,word_query_module.data->letter_focus_index);
//    lv_obj_add_state(focus_obj_img_parent, LV_STATE_CHECKED);
//    lv_obj_t *focus_obj_img = lv_obj_get_child(focus_obj_img_parent,0);
//    lv_obj_add_flag(focus_obj_img, LV_OBJ_FLAG_HIDDEN);
    lv_obj_t *focus_obj = lv_obj_get_child(focus_obj_img_parent,1);
    lv_group_focus_obj(focus_obj);

    fresh_letter_parent_style(index);
}

// 根据最近查询单词列表刷新界面
static void fresh_recently_word_list(word_query_word_entry_t *word_list,int size,lv_obj_t *prev_obj,int pad_row){
    LOG_INFO_WRAPPER("start");
    // 获取单词列表panel
    lv_obj_t *word_list_panel = common_ui_comp_get_child(word_query_module.screen->screen,UI_COMP_WORD_QUERY_LEFT_WORD_LIST_PANEL);
    lv_obj_set_style_pad_row(word_list_panel, pad_row, 0);

    // 清除子节点
    lv_obj_clean(word_list_panel);
    // 在event_data中保存上一个和下一个word_list_child_panel
    lv_obj_t *prev_word_list_child_panel = NULL;
    // 获取第一个字母父节点
    lv_obj_t *middle_right_left_panel = common_ui_comp_get_child(word_query_module.screen->screen,UI_COMP_WORD_QUERY_LETTER_PARENT_PANEL);
    lv_obj_t *middle_right_left_child_panel = lv_obj_get_child(middle_right_left_panel, 0);
    lv_obj_t *middle_right_left_child_label_panel = lv_obj_get_child(middle_right_left_child_panel, 0);
    // 填充子节点
    int first_node_index = 0;
    for (int i = 0; i < size; ++i) {
        lv_obj_t *word_list_child_panel = ui_create_without_flex_panel(word_list_panel,LV_SIZE_CONTENT,20);
        lv_obj_set_style_pad_top(word_list_child_panel, -6, 0);
        lv_obj_set_style_border_side(word_list_child_panel, LV_BORDER_SIDE_BOTTOM,LV_PART_MAIN|LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(word_list_child_panel, 2, LV_PART_MAIN|LV_STATE_FOCUSED);
        lv_obj_set_style_pad_left(word_list_child_panel,-2,LV_PART_MAIN|LV_STATE_FOCUSED);



        lv_obj_t *word_list_child_label = ui_create_default_label(word_list_child_panel,(char *)word_list[i].word,CHINESE_FONT_16_LIGHT);
        // 如果是最近查询单词，则不加入焦点组和事件
        if(!strncmp(word_list[i].word,"最近查询单词:",WORD_QUERY_MAX_WORD_LEN)){
            first_node_index++;
            continue;
        }
        word_query_event_data_t* event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        event_data->index = i;
        event_data->obj_type = UI_WORD_QUERY_OBJ_TYPE_WORD_LIST_CHILD;
        event_data->prev_obj = prev_word_list_child_panel;
        // 让前一个 panel 的 next 指向当前 panel
        if (prev_word_list_child_panel){
            word_query_event_data_t *prev_event_data = lv_obj_get_user_data(prev_word_list_child_panel);
            if (prev_event_data) {
                prev_event_data->next_obj = word_list_child_panel;
            }
        }
        word_query_add_event_common(word_list_child_panel,event_data);
//        lv_group_add_obj(word_query_module.screen->group, word_list_child_panel);
//        lv_obj_set_user_data(word_list_child_panel, event_data);
//        lv_obj_add_event_cb(word_list_child_panel, ui_word_query_main_screen_event,LV_EVENT_KEY, event_data);
//        lv_obj_add_event_cb(word_list_child_panel, common_del_event_data_event_cb, LV_EVENT_DELETE,
//                            event_data);
        // 更新 prev 变量，为下一次循环做准备
        prev_word_list_child_panel = word_list_child_panel;
        //最后一个的next 指向第一个letter_parent_panel
        if(i == size - 1){


            event_data->next_obj = middle_right_left_child_label_panel;
            // 第一个字母父节点的上一个节点指向最后一个单词列表子节点
            word_query_event_data_t *word_query_event_data = lv_obj_get_user_data(middle_right_left_child_label_panel);
            if(word_query_event_data){
                word_query_event_data->prev_obj = word_list_child_panel;
            }
        }

        // 如果是第一个节点，那么上一个节点就是prev_obj
        if(i == first_node_index){
            event_data->prev_obj = prev_obj;
            // prev_obj的下一个节点就是当前节点
            word_query_event_data_t *word_query_event_data = lv_obj_get_user_data(prev_obj);
            word_query_event_data->next_obj = word_list_child_panel;
        }
    }

    /**
     * 单词列表没元素时
     * 第一个字母父节点的上一个节点指向prev_obj
     * prev_obj的下一个节点指向第一个字母父节点
     */
    int list_is_null = false;
     if(first_node_index==0 && size==0){
         list_is_null = true;

     }else if(first_node_index==1 && size==1){
         list_is_null = true;
     }
     if(list_is_null){
         word_query_event_data_t *word_query_event_data = lv_obj_get_user_data(middle_right_left_child_label_panel);
         if(word_query_event_data){
             word_query_event_data->prev_obj = prev_obj;
         }
         word_query_event_data = lv_obj_get_user_data(prev_obj);
         word_query_event_data->next_obj = middle_right_left_child_label_panel;
     }
}

// 根据输入框文本长度展示输入框
static void fresh_input_box_panel() {
    lv_obj_t *no_content_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                          UI_COMP_WORD_QUERY_INPUT_BOX_PANEL);
    lv_obj_t *with_content_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                          UI_COMP_WORD_QUERY_INPUT_BOX_CONTENT_PANEL);
    lv_obj_t *delete_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                      UI_COMP_WORD_QUERY_INPUT_DELETE_BUTTON_PANEL);
    word_query_event_data_t *event_data = lv_obj_get_user_data(no_content_panel);
    lv_obj_t *return_panel = event_data->prev_obj;
    event_data = lv_obj_get_user_data(return_panel);
    if(word_query_module.data->input_word_len > 0){
        // 显示带内容的输入框
        lv_obj_clear_flag(with_content_panel, LV_OBJ_FLAG_HIDDEN);
        lv_obj_add_flag(no_content_panel, LV_OBJ_FLAG_HIDDEN);
        // 返回的panel的下一个panel 是delete_panel
        event_data->next_obj = delete_panel;
    }else{
        lv_obj_add_flag(with_content_panel, LV_OBJ_FLAG_HIDDEN);
        lv_obj_clear_flag(no_content_panel, LV_OBJ_FLAG_HIDDEN);
        // 返回的panel的下一个panel 是no_content_panel
        event_data->next_obj = no_content_panel;

    }
}

// 根据数据刷新界面
void ui_word_query_fresh_screen(){
    LOG_INFO_WRAPPER("start");
    fresh_letter_by_parent_index(word_query_module.data->letter_parent_index);

    ui_fresh_search_word_list();
    // 获取输入框
//    lv_obj_t *input_box_panel = common_ui_comp_get_child(word_query_module.screen->screen,
//                                                         UI_COMP_WORD_QUERY_INPUT_BOX_PANEL);
//    fresh_recently_word_list(word_query_module.data->word_list, word_query_module.data->word_list_size,input_box_panel,WORD_QUERY_RECENTLY_WORD_LIST_PAD_ROW);
//
//    // 根据输入框文本长度展示输入框
//    fresh_input_box_panel();
}

// 根据查找单词刷新单词列表
static void ui_fresh_search_word_list() {
    // 搜索单词列表
    if(word_query_module.data->word_list){
        free(word_query_module.data->word_list);
        word_query_module.data->word_list = NULL;
    }
    int pad_row = 0;
    // 获取输入框
    lv_obj_t *input_box_panel = NULL;
    if(word_query_module.data->input_word_len > 0){
        word_query_module.data->word_list = word_query_search_word_list(word_query_module.data->input_word,&(word_query_module.data->word_list_size),5);
        pad_row = WORD_QUERY_SEARCH_WORD_LIST_PAD_ROW;
        // 显示input_word
        lv_obj_t *input_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                         UI_COMP_WORD_QUERY_INPUT_BOX_CONTENT_PANEL);
        lv_obj_t *input_label = lv_obj_get_child(lv_obj_get_child(input_panel, 0),0);
        lv_label_set_text(input_label, word_query_module.data->input_word);
        input_box_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                   UI_COMP_WORD_QUERY_INPUT_DELETE_BUTTON_PANEL);
    }else{
        word_query_module.data->word_list = word_query_get_recently_word_list(&(word_query_module.data->word_list_size),6);
        pad_row = WORD_QUERY_RECENTLY_WORD_LIST_PAD_ROW;
        input_box_panel = common_ui_comp_get_child(word_query_module.screen->screen,
                                                   UI_COMP_WORD_QUERY_INPUT_BOX_PANEL);
    }
    // 刷新单词列表

    fresh_recently_word_list(word_query_module.data->word_list, word_query_module.data->word_list_size,input_box_panel,pad_row);


    fresh_input_box_panel();

}

// 隐藏或显示输入框dash图片
static void ui_hidden_input_box_dash_img(lv_obj_t *obj,bool is_hidden) {
// 判断带不带删除panel
    word_query_event_data_t *input_panel_event_data = lv_obj_get_user_data(obj);
    if(input_panel_event_data->obj_type == UI_WORD_QUERY_OBJ_TYPE_INPUT_BOX_PANEL){

        lv_obj_t *dash_img = lv_obj_get_child(obj, 0);
        if(is_hidden){
            lv_obj_add_flag(dash_img, LV_OBJ_FLAG_HIDDEN);
        }else{
            lv_obj_clear_flag(dash_img, LV_OBJ_FLAG_HIDDEN);
        }
    }else if(input_panel_event_data->obj_type == UI_WORD_QUERY_OBJ_TYPE_DELETE_PANEL){
        //带删除 checked parent，隐藏dash
//                lv_obj_t *delete_parent_panel = lv_obj_get_child(input_panel_event_data->prev_parent_obj,0);
        lv_obj_t *delete_parent_panel = input_panel_event_data->prev_parent_obj;
        lv_obj_t *dash_img = lv_obj_get_child(delete_parent_panel, 0);
        if(is_hidden){
            lv_obj_add_state(delete_parent_panel, LV_STATE_CHECKED);
            lv_obj_add_flag(dash_img, LV_OBJ_FLAG_HIDDEN);
        }else{
            lv_obj_clear_state(delete_parent_panel,LV_STATE_CHECKED);
            lv_obj_clear_flag(dash_img, LV_OBJ_FLAG_HIDDEN);
        }

    }
}






// 界面初始化
void ui_word_query_main_init(ui_word_query_main_screen_t *screen) {
    LOG_INFO_WRAPPER("start");
    if(screen->screen == NULL){
        lv_obj_t **children = lv_mem_alloc(sizeof(lv_obj_t *) * UI_COMP_WORD_QUERY_SCREEN_NUM);
        if(children == NULL){
            LV_LOG_ERROR("ui_WordSpellScreen_screen_init children malloc failed");
            return;
        }
        if(screen->group == NULL){
            screen->group = ui_set_indev_group(indev_keypad);
        }
        screen->screen = ui_create_default_screen(lv_scr_act());

        // 主体列对齐panel
        lv_obj_t *main_panel = ui_create_column_default_panel(screen->screen);


        // 顶部行对齐panel
        lv_obj_t *top_panel = ui_word_query_top_panel_component(main_panel);

        // 返回label
        lv_obj_t *top_return_panel = lv_obj_get_child(top_panel, 1);
        lv_obj_set_style_border_side(top_return_panel, LV_BORDER_SIDE_BOTTOM,LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(top_return_panel, 2, LV_STATE_FOCUSED);
        word_query_event_data_t *top_panel_event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        top_panel_event_data->obj_type = UI_WORD_QUERY_OBJ_TYPE_TOP_PANEL_LABEL;
        word_query_add_event_common(top_return_panel, top_panel_event_data);


        // 中间行对齐panel all 宽 210 高
        lv_obj_t *middle_panel = ui_create_row_panel(main_panel, lv_disp_get_hor_res(NULL),210);

        // 中间左边列对齐 panel 宽181 高211 有border width 1 row_padding 12
        lv_obj_t *middle_left_panel = ui_create_column_panel(middle_panel, 181, 211);
        lv_obj_set_flex_align(middle_left_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START,
                              LV_FLEX_ALIGN_START);
        lv_obj_set_style_border_side(middle_left_panel, LV_BORDER_SIDE_RIGHT, 0);
        lv_obj_set_style_border_width(middle_left_panel, 1, 0);
        lv_obj_set_style_pad_row(middle_left_panel, 12, 0);
        lv_obj_set_style_pad_left(middle_left_panel, 8, 0);
        lv_obj_set_style_pad_top(middle_left_panel, 10, 0);

        // 中间左边列元素
        // 输入框
        // 无内容时的输入框
        lv_obj_t *input_box_panel = ui_create_without_flex_panel(middle_left_panel,167,28);
        children[UI_COMP_WORD_QUERY_INPUT_BOX_PANEL] = input_box_panel;
//        lv_obj_add_flag(input_box_panel, LV_OBJ_FLAG_HIDDEN);
        lv_obj_t *input_box_img = ui_create_default_image_by_src(input_box_panel,&dash_167_28_img);
        lv_obj_t *input_box_label = ui_create_default_label(input_box_panel,"请输入要查找的单词",CHINESE_FONT_16_LIGHT);
        lv_obj_set_style_text_letter_space(input_box_panel, 2, 0);
        lv_obj_set_style_border_side(input_box_panel, LV_BORDER_SIDE_FULL,LV_STATE_FOCUSED);
        lv_obj_set_style_border_width(input_box_panel, 1, LV_STATE_FOCUSED);
        word_query_event_data_t *input_box_event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        input_box_event_data->obj_type = UI_WORD_QUERY_OBJ_TYPE_INPUT_BOX_PANEL;
        input_box_event_data->prev_obj = top_return_panel;
//        top_panel_event_data->next_obj = input_box_panel;
        word_query_add_event_common(input_box_panel,input_box_event_data);


        //有内容时的输入框
        lv_obj_t *input_box_content_panel = ui_create_row_panel(middle_left_panel, 167, 28);
        children[UI_COMP_WORD_QUERY_INPUT_BOX_CONTENT_PANEL] = input_box_content_panel;
//        lv_obj_add_flag(input_box_content_panel, LV_OBJ_FLAG_HIDDEN);
        lv_obj_set_style_pad_column(input_box_content_panel, 2, 0);
        lv_obj_t *input_box_content_left_panel = ui_create_without_flex_panel(input_box_content_panel,126,28);
        lv_obj_set_style_border_side(input_box_content_left_panel, LV_BORDER_SIDE_FULL, 0);
        lv_obj_set_style_border_width(input_box_content_left_panel, 1, 0);
        lv_obj_set_style_pad_left(input_box_content_left_panel, 4, 0);
        lv_obj_t *input_box_content_left_label = ui_create_default_label(input_box_content_left_panel,"s",CHINESE_FONT_16_LIGHT);
        lv_obj_set_align(input_box_content_left_label, LV_ALIGN_LEFT_MID);

        // 删除按钮
        lv_obj_t *input_box_content_right_panel = ui_create_without_flex_panel(input_box_content_panel,39,28);
        lv_obj_set_style_border_width(input_box_content_right_panel, 1, LV_STATE_CHECKED);
        lv_obj_set_style_border_side(input_box_content_right_panel, LV_BORDER_SIDE_FULL, LV_STATE_CHECKED);
//        lv_obj_add_state(input_box_content_right_panel, LV_STATE_CHECKED);
        lv_obj_t *input_box_content_right_img = ui_create_default_image_by_src(input_box_content_right_panel,&dash_39_28_img);
//        lv_obj_add_flag(input_box_content_right_img, LV_OBJ_FLAG_HIDDEN);
        lv_obj_t *input_box_content_right_child_panel = ui_create_without_flex_panel(input_box_content_right_panel,32,22);
        children[UI_COMP_WORD_QUERY_INPUT_DELETE_BUTTON_PANEL] = input_box_content_right_child_panel;
        lv_obj_set_align(input_box_content_right_child_panel, LV_ALIGN_CENTER);
        lv_obj_set_style_pad_top(input_box_content_right_child_panel, -2, 0);
        lv_obj_set_style_border_width(input_box_content_right_child_panel, 2, LV_STATE_FOCUSED);
        lv_obj_set_style_border_side(input_box_content_right_child_panel, LV_BORDER_SIDE_BOTTOM, LV_STATE_FOCUSED);
//        lv_obj_add_state(input_box_content_right_child_panel, LV_STATE_CHECKED);
        lv_obj_t *input_box_content_right_child_label = ui_create_default_label(input_box_content_right_child_panel,"删除",CHINESE_FONT_16_LIGHT);
        input_box_event_data = malloc_set_zero(sizeof(word_query_event_data_t));
        input_box_event_data->obj_type = UI_WORD_QUERY_OBJ_TYPE_DELETE_PANEL;
        input_box_event_data->prev_obj = top_return_panel;
        input_box_event_data->prev_parent_obj = input_box_content_right_panel;
        input_box_event_data->next_parent_obj = input_box_panel;
//        top_panel_event_data->next_obj = input_box_panel;
        word_query_add_event_common(input_box_content_right_child_panel,input_box_event_data);



        // 底部单词列表 141 161 左边对齐
//        lv_obj_t *middle_left_word_list_panel = ui_create_column_panel(middle_left_panel,141,161);
        lv_obj_t *middle_left_word_list_panel = ui_create_column_panel(middle_left_panel,LV_SIZE_CONTENT,161);
        lv_obj_set_flex_align(middle_left_word_list_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START,
                              LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_left(middle_left_word_list_panel, 3, 0);
//        lv_obj_set_style_border_side(middle_left_word_list_panel, LV_BORDER_SIDE_TOP,0);
//        lv_obj_set_style_border_width(middle_left_word_list_panel, 1, 0);
        lv_obj_set_style_pad_row(middle_left_word_list_panel, WORD_QUERY_RECENTLY_WORD_LIST_PAD_ROW, 0);
        children[UI_COMP_WORD_QUERY_LEFT_WORD_LIST_PANEL] = middle_left_word_list_panel;


        // 中间右边行对齐 panel 宽416-181 高211
        lv_obj_t *middle_right_panel = ui_create_row_panel(middle_panel, lv_disp_get_hor_res(NULL)-181,211);
        lv_obj_set_style_pad_top(middle_right_panel, 10, 0);
        lv_obj_set_style_pad_left(middle_right_panel, 5, 0);
        lv_obj_set_flex_align(middle_right_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_column(middle_right_panel, 7, 0);
        // 左边列 列对齐元素

        lv_obj_t *middle_right_left_panel = ui_create_column_default_panel(middle_right_panel);
        children[UI_COMP_WORD_QUERY_LETTER_PARENT_PANEL] = middle_right_left_panel;
        lv_obj_set_flex_align(middle_right_left_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_row(middle_right_left_panel, 20, 0);
        // 在event_data中保存上一个和下一个middle_right_left_child_label_panel
        lv_obj_t *prev_middle_right_left_child_label_panel = NULL;
        lv_obj_t *first_middle_right_left_child_label_panel = NULL;
        char *middle_right_left_label_text[] = {"abc","ABC","123","字符"};
        for (int i = 0; i < sizeof(middle_right_left_label_text) / sizeof(char *); ++i) {
            lv_obj_t *middle_right_left_child_panel = ui_create_without_flex_panel(middle_right_left_panel,42,27);
            lv_obj_set_style_border_side(middle_right_left_child_panel, LV_BORDER_SIDE_FULL, 0);
            lv_obj_set_style_border_width(middle_right_left_child_panel, 1, 0);
            lv_obj_set_style_bg_color(middle_right_left_child_panel, lv_color_hex(0x000000), LV_STATE_CHECKED);

            // label的父panel
            lv_obj_t *middle_right_left_child_label_panel = ui_create_without_flex_panel(middle_right_left_child_panel,34,24);
            if(i == 0){
                first_middle_right_left_child_label_panel = middle_right_left_child_label_panel;
            }
            lv_obj_set_style_border_width(middle_right_left_child_label_panel, 2, LV_STATE_FOCUSED);
            lv_obj_set_style_border_side(middle_right_left_child_label_panel, LV_BORDER_SIDE_BOTTOM, LV_STATE_FOCUSED);
            lv_obj_set_style_border_color(middle_right_left_child_label_panel, lv_color_white(), LV_STATE_CHECKED);
            // 完全透明，背景可见
            lv_obj_set_style_bg_opa(middle_right_left_child_label_panel, LV_OPA_0, 0);

//            lv_obj_set_style_bg_color(middle_right_left_child_panel, lv_color_hex(0xffffff), 0);
            lv_obj_t *middle_right_left_child_label = ui_create_default_label(middle_right_left_child_label_panel,middle_right_left_label_text[i],CHINESE_FONT_16_LIGHT);
            lv_obj_set_style_text_color(middle_right_left_child_label, lv_color_hex(0xffffff), LV_STATE_CHECKED);


            word_query_event_data_t* event_data = malloc_set_zero(sizeof(word_query_event_data_t));
            event_data->index = i;
            event_data->obj_type = UI_WORD_QUERY_OBJ_TYPE_LETTER_PARENT;
            event_data->prev_obj = prev_middle_right_left_child_label_panel;
            // 让前一个 panel 的 next 指向当前 panel
            if(prev_middle_right_left_child_label_panel){
                word_query_event_data_t *prev_event_data = lv_obj_get_user_data(prev_middle_right_left_child_label_panel);
                if (prev_event_data) {
                    prev_event_data->next_obj = middle_right_left_child_label_panel;
                }
            }
            word_query_add_event_common(middle_right_left_child_label_panel,event_data);

            // 更新 prev 变量，为下一次循环做准备
            prev_middle_right_left_child_label_panel = middle_right_left_child_label_panel;
            //最后一个的next指向第一个
            if(i == sizeof(middle_right_left_label_text) / sizeof(char *) - 1){
                event_data->next_obj = first_middle_right_left_child_label_panel;
            }
        }

        // 右边列 行对齐元素
//        lv_obj_t *middle_right_right_panel = ui_create_row_default_panel(middle_right_panel);
        lv_obj_t *middle_right_right_panel = ui_create_row_panel(middle_right_panel, 181,LV_SIZE_CONTENT);
        // 可换行
        lv_obj_set_flex_flow(middle_right_right_panel, LV_FLEX_FLOW_ROW_WRAP);
        lv_obj_set_flex_align(middle_right_right_panel, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_START);
        lv_obj_set_style_pad_column(middle_right_right_panel, 5, 0);
        lv_obj_set_style_pad_row(middle_right_right_panel, 14, 0);
        children[UI_COMP_WORD_QUERY_LETTER_PARENT_LIST_PANEL] = middle_right_right_panel;



        lv_obj_add_event_cb(screen->screen,common_get_component_child_event_cb,COMMON_LV_EVENT_GET_COMP_CHILD,children);
        lv_obj_add_event_cb(screen->screen,common_del_component_child_event_cb,LV_EVENT_DELETE,children);

        // 焦点组回调
        lv_group_set_focus_cb(screen->group,ui_word_query_focus_cb);

    }
}

void ui_word_query_main_screen_show(void) {
    LOG_INFO_WRAPPER("start");
    if(word_query_module.screen->screen == NULL){
//        ui_word_query_data_init(word_query_module.data);
        ui_word_query_main_init(word_query_module.screen);
        ui_word_query_fresh_screen(word_query_module);
    }
    word_query_module.screen->deinit_cb = ui_word_query_main_screen_deinit;
    LOG_INFO_WRAPPER("end");
}

void ui_word_query_main_screen_deinit(void) {
    LOG_INFO_WRAPPER("start");
    ui_screen_group_deinit_common(&(word_query_module.screen->screen),&(word_query_module.screen->group));
    LOG_INFO_WRAPPER("end");
//    void ui_desktop_show();
//    ui_desktop_show();

}

void ui_word_query_screen_show(){
    ui_word_query_data_init(&ui_word_query_data);
//    if(ui_word_query_data.is_with_keyboard){
    if(0){
        ui_word_query_with_keyboard_show();
    }else{
        ui_word_query_main_screen_show();
    }
}
